﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using RZD_maps.Modules.Graphics.Views;
using RZD_maps.Modules.Graphics.Model;

using Microsoft.Practices.Prism.Events;
using RZD_maps.Infrastructure;
using System.Diagnostics;

namespace RZD_maps.Modules.Graphics.ViewModels
{

    public class DataGenerator: ViewModelBase

    {

        private static int grmode = 1;

        public DataGenerator(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            SubscriptCommands();
        }

        public void SubscriptCommands()
        {
            ControlDataEvent evnt = eventAggregator.GetEvent<ControlDataEvent>();
            InfoForGraphicsEvent evntInfo = eventAggregator.GetEvent<InfoForGraphicsEvent>();

            if (subscriptionToken != null)
            {
                evnt.Unsubscribe(subscriptionToken);
            }

            if (subscriptionTokenGraphicName != null)
            {
                evntInfo.Unsubscribe(subscriptionTokenGraphicName);
            }

            subscriptionToken = evnt.Subscribe(ControlDataEventHandler, ThreadOption.UIThread, false, ControlDataFilter);
            subscriptionTokenGraphicName = evntInfo.Subscribe(GraphicsDataEventHandler, ThreadOption.UIThread, false, GraphicsDataFilter);
        }

        public void ControlDataEventHandler(ControlDataItem controlDataItem)
        {
            Debug.Assert(View != null);
                switch (controlDataItem.command)
                {
                    case ControlDataCommandEnum.gr1:
                        grmode = 1;
                        break;
                    case ControlDataCommandEnum.gr2:
                        grmode = 2;
                        break;
                    case ControlDataCommandEnum.gr3:
                        grmode = 3;

                        break;
                }
            
        }

        public void GraphicsDataEventHandler(InfoForGraphics item)
        {
            Debug.Assert(View != null);
            ((View)View).LegendData.Header = item.name;
        }

        public bool GraphicsDataFilter(InfoForGraphics item)
        {
            return true;
        }

        public bool ControlDataFilter(ControlDataItem controlData)
        {
            return true;
        }

        private IEventAggregator eventAggregator;
        private SubscriptionToken subscriptionToken;
        private SubscriptionToken subscriptionTokenGraphicName;

         private IView view;

         public IView View
         {
             get { return view; }
             set
             {
                 view = value;
             }
         }

                private ObservableCollection<Data> collection;
        public ObservableCollection<Data> Collection 
        {
            get { return collection; }
            set { collection = value; OnPropertyChanged("Collection");}
        }

        private ObservableCollection<Data> collection1;
        public ObservableCollection<Data> Collection1
        {
            get { return collection1; }
            set { collection1 = value; OnPropertyChanged("Collection1"); }
        }

        private DispatcherTimer timer;
        private double phase;
        static double increment = Math.PI * 0.1;
        public DataGenerator()
        {
            Collection = Datas.RandomSeries(2.0, phase, 1000);
            Collection1 = Datas.RandomSeries(1.0, phase, 1000);
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(2000);
            timer.Tick += OnTick;
            timer.Start();
        }

        void OnTick(object sender, EventArgs e)
        {
                switch (grmode)
                {
                    case 1:
                        Collection1 = Datas.RandomSeries(2.0, phase, 1000);
                        Collection = Datas.RandomSeries(1.0, phase, 1000);
                        phase += increment;
                        break;
                    case 2:
                        Collection1 = Datas.RandomSeries1(2.0, phase, 1000);
                        Collection = Datas.RandomSeries1(1.0, phase, 1000);
                        phase += increment;
                        break;
                    case 3:
                        Collection1 = Datas.RandomSeries2(2.0, phase, 1000);
                        Collection = Datas.RandomSeries2(1.0, phase, 1000);
                        phase += increment;
                        break;
                }
          //timer.Stop();
        }
    }
}
